package ro.gateway.aida.charts;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieItemLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.DefaultPieDataset;

import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.Module_DB;
import ro.xblue.translator.TranslatorConstants;
import ro.xblue.translator.TranslatorUtils;
import ro.xblue.translator.Translator_DB;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jun 20, 2004
 * Time: 12:12:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class PieChartRenderer {
	public static void make_piechart(
		  HttpServletRequest request, ServletContext application,
		  ChartWizzard$ActionForm form)
		  throws IOException, ChartException {
		HttpSession session = request.getSession();

//		System.out.println("creating chart");
		LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request, application);
		Translator_DB translator_db = (Translator_DB) application.
			  getAttribute(TranslatorConstants.DB_TRANSLATOR);
		Module_DB tr_module = translator_db.getModule("charts");

		// dataset
		DefaultPieDataset pie_dataset = new DefaultPieDataset();
		PieChartDataMan.populate_pie_dataset(pie_dataset, lang_bean.language, form);
		if (pie_dataset.getItemCount() < 1) {
			throw new ChartException("No data to display");
		}


		// plot
		PiePlot plot = new PiePlot(pie_dataset);
		plot.setToolTipGenerator(new StandardPieItemLabelGenerator());

		// chart
		JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT,
			  plot, form.x_axe_group_criteria != null);
		chart.setBackgroundPaint(java.awt.Color.white);
		chart.setAntiAlias(true);
//		RenderingHints hints = chart.getRenderingHints();

		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		form.file_name = ServletUtilities.saveChartAsPNG(chart,
			  form.chartwidth, form.chartheight,
			  info, session);

		form.tooltipmap = ChartUtilities.getImageMap(form.file_name, info);

//		System.out.println("created chart:" + form.file_name);
	}

}
